package cn.xdf.Demo01;

import cn.xdf.dao.UserDao;
import cn.xdf.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class demo01 {

    @Autowired
    private UserDao userDao;

    @Test//增
    public void test01(){
        User user = new User(null, "xioamingming", "2003-10-10", "女", "广州");
        userDao.save(user);
    }

    @Test//删
    public void test02(){
       userDao.deleteById(9);
    }

    @Test//改
    public void test03(){
        User user = new User(7, "xioahuihui", "2003-10-10", "女", "广州");
        userDao.save(user);
    }

    @Test//查
    public void test04(){
        List<User> userList = userDao.findAll();
        for (User user:userList){
            System.out.println(user);
        }
    }

    @Test//分页查询
    public void test05(){

    /*
    1.页码，默认从0开始算
    2.页大小
     */
        Integer page=2;//表示第2页了
        Integer size=3;
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        //查询分页信息
        Page<User> pageData = userDao.findAll(pageRequest);

        System.out.println("总页数: " + pageData.getTotalPages());
        System.out.println("总记录数: " + pageData.getTotalElements());

        //当前页数据
        List<User> userList = pageData.getContent();

        for (User user : userList) {
            System.out.println(user);
        }

    }

    @Test
    public void test06(){
        //根据id降序
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        List<User> userList = userDao.findAll(sort);

        for(User user :userList){
            System.out.println(user);
        }
    }

    @Test
    public void test07(){

        User user = userDao.findById_JPQL(10);
        System.out.println(user);
    }

    @Test
    @Transactional
    @Rollback(false)
    public void test08(){
        userDao.update_JPQL("小明",3);
    }

    @Test
    public void test09(){
        User user = userDao.findById_SQL(10);
        System.out.println(user);
    }

    @Test
    @Transactional
    @Rollback(false)
    public void test10(){
        userDao.update_SQL("小美",4);
    }




}
